package com.steamedfish.auctioncloudadserver.task;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.steamedfish.auctioncloudadserver.entiry.AdMailEntity;
import com.steamedfish.auctioncloudadserver.entiry.UserEntity;
import com.steamedfish.auctioncloudadserver.mapper.AdMailMapper;
import com.steamedfish.auctioncloudadserver.mapper.UserMapper;
import com.steamedfish.auctioncloudadserver.service.AdMailService;
import com.steamedfish.auctioncloudadserver.service.elasticsearch.ElasticSearchService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class AuctionPromotionTask {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AdMailMapper adMailMapper;

    @Autowired
    private AdMailService adMailService;

    @Autowired
    ElasticSearchService elasticSearchService;

    @Autowired
    private MailSender mailSender;

    @Autowired
    private SimpleMailMessage simpleMailMessage;

    @Value(value = "${task.promotion.auction:0/5 * * * * *}")
    private String PROMOTION_AUCTION;


    /**
     * 定期推广新上传的商品信息给用户
     *
     * @param id
     * @return int
     */
    @Scheduled(cron = "0/30 * * * * *")
    public void newAuctionPromotion() {
        logger.info("发送商品推广信息");
        //获取推广商品信息
        List<String> ids = this.getAuctionList("ad-for-auction");
        Map<String, String> map = elasticSearchService.searchByListId(ids);
        if (map.size() > 0) {
            String context = this.getContext(map);//获取推广邮件地址
            List<String> mailList = adMailService.getMailAddress();//发送邮件
            adMailService.sendMail(mailList, "蒸鱼校园拍买平台拍品上新提醒", context);
        }
    }





    /** 从redis获取待推广的商品信息
     *
     * @param id
     * @return int
     */
    private List<String> getAuctionList(String key){
        Set<String> set = redisTemplate.opsForSet().members(key);
        List<String> aulist = new ArrayList<>(set);
        if (set.size() > 0) {
            for (String id : aulist) {
                redisTemplate.opsForSet().remove(key, id);
            }
        }
        return aulist;
    }


    /**
     * 生成发送内容
     *
     * @param id
     * @return int
     */
    private String getContext(Map<String, String> map) {
        //创建发送内容
        String codeText = "新品上新，快来看看有没有您中意的小商品\n\n";
        for (Map.Entry<String, String> entry : map.entrySet()) {
            codeText = codeText + entry.getKey() + "：" + entry.getValue() + "\n\n";
        }
        return codeText;
    }


}